<?php
include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
include_once("$config[webroot]/lib/Httpsqs.php");
//================================================================
$pro=new pro();
if($_GET['operation'] && $_GET['operation']=='getCatByHscode'){
	$customs_cat = array();
	if(!empty($_GET['hscode'])){
		$customs_cat = $pro->get_product_cat_customs($_GET['hscode']);
		if(!empty($customs_cat)){
			die(json_encode($customs_cat));
		}
	}
	exit();
}
if(empty($_GET['catid'])&&empty($_GET['edit']))
{	
	$sql="select * from ".PCAT." where catid<9999 order by displayorder asc";
	$db->query($sql);
	$re=$db->getRows();
	$tpl->assign("cat",$re);
	//------------------------------
	
	$tpl->assign("config",$config);
	$tpl->assign("lang",$lang);
	$tpl->display("product_step.htm");
}
else
{	
	//-------------------------------------
	if($_POST['submit']=="submit")
	{	
		unset($_GET['m']);
		unset($_GET['s']);
		unset($_GET['category_id']);
		unset($_GET['catid']);
		unset($_GET['tcatid']);
		unset($_GET['scatid']);
		unset($_GET['sscatid']);
		
		
		$re=$pro->add_pro();
		if($re){
			Httpsqs::put('solrProducts',array('product_id'=>$re));
		    if($_POST['returnself']){
		        msg("module.php?m=product&s=product_launch.php&edit=".$re);
		    }else{
		        msg("module.php?m=product&s=product.php&".http_build_query($_GET));
		    }
		    
		}
	}
	//-------------------------------------
	if($_POST['submit']=="edit")
	{
		unset($_GET['m']);
		unset($_GET['s']);
		unset($_GET['edit']);
		unset($_GET['category_id']);
		unset($_GET['catid']);
		unset($_GET['tcatid']);
		unset($_GET['scatid']);
		unset($_GET['sscatid']);
		
		
		$re=$pro->edit_pro();
		
		if($re){
			Httpsqs::put('solrProducts',array('product_id'=>$re));
		    if($_POST['returnself']){
		        msg("module.php?".$_SERVER['QUERY_STRING']);
		    }else{
		        msg("module.php?m=product&s=product.php&".http_build_query($_GET));
		    }
		    
		}
	}
	//------------------------------------

	if(!empty($_GET['edit']))
	{	
		$de=$pro->pro_detail($_GET['edit']);
		
		//海关分类
		$customs_cat = $pro->get_product_cat_customs();
		
		$tpl->assign("de",$de);
		$tpl->assign("customs_cat",$customs_cat);
		$pactidlist=$de['catid'];
		if(!empty($de['tcatid']))
			$pactidlist.=",".$de['tcatid'];
		if(!empty($de['scatid']))
			$pactidlist.=",".$de['scatid'];	
		if(!empty($de['sscatid']))
			$pactidlist.=",".$de['sscatid'];
	}
	//--------------------------------
	if(!empty($_GET['catid']))
	{
		$pactidlist=!empty($_GET['catid'])?$_GET['catid']:NULL;
		if(!empty($_GET['tcatid']))
			$pactidlist.= ",".$_GET['tcatid'];
		if(!empty($_GET['scatid']))
			$pactidlist.=",".$_GET['scatid'];
		if(!empty($_GET['sscatid']))
			$pactidlist.=",".$_GET['sscatid'];
	}
	
	$tpl->assign("typenames",$pro->getProTypeName($pactidlist));
	//$tpl->assign("brand",$pro->get_brand($pactidlist,$de['brand']));
	
	$tpl->assign("tags",$pro->get_tags());
	
	$tpl->assign("tax_rate",$pro->get_tax_rate($pactidlist));//税率
	
	
	$tpl->assign("stock_shops",$pro->get_stock_shops());
	
	$tpl->assign("prov",GetDistrict());
	//--------------------------自定义字段
	$nc=explode(",",$pactidlist);
	$now_catid=$nc[count($nc)-1];
	
	$sql="select ext_table,ext_field_cat from ".PCAT." where catid='$now_catid'";
	$db->query($sql);
	$re=$db->fetchRow();
	$ext_table=$re['ext_table'];
	$property_id=$re['ext_field_cat'];
	
	//hscode
	$sql = "select hscode from ".PCAT." where catid='$now_catid'";
	$db->query($sql);
	$res=$db->fetchRow();
	$tpl->assign('hscode',$res['hscode']);
	
	include_once("$config[webroot]/module/product/includes/plugin_add_field_class.php");
	$addfield = new AddField('product');
	$extfiled=$addfield->addfieldinput($_GET['edit'],$ext_table);//
	$abc=$addfield->echoforeach('0',count($extfiled['d']));//

	
	//--------------------------------通用扩展字段
	$db->query("SHOW TABLES LIKE '".$config['table_pre']."product_defind'");
	if($db->num_rows()==1)
	{
		if ($_GET['edit']*1>"0"){
			$sql="select * from ".$config['table_pre']."product_defind where pid='".$_GET['edit']."'";
			$db->query($sql);
			$de=$db->fetchRow();
		}
		
		
		
		$tsql="select id,catName,catDes,catInfo,defaultValue,fieldPro,displayType,catItem 
			 from ".EXTENDFILE." where module='product' order by id asc";
		
		$db->query($tsql);
		$res=$db->getRows();
		$j=1;
		foreach($res as $v)
		{
			$t=array();
			$t['j']=$j;
			$t["fieldName"]=$v["catName"];//字段名称
			$t["fieldDisplay"]=$v["catInfo"];//字段描述
			$t["fieldDesc"]=$v["catDes"];//字段描述
				
			if($_GET['edit']*1>"0")
				$t["defaultValue"]=$de[$v['catName']];//编辑状态
			else
				$t["defaultValue"]=$v['defaultValue'];//默认值
			$t["displayType"]=$v["displayType"];//显示类型
			$t["catItem"]=$v["catItem"];//列表值
				
			$allextfield[]=$addfield->addtabletr($t);
		}
		
		$tpl->assign("allextfield",$allextfield);
		

	}
	$tsql="select * from ".PROVINCE." where 1=1";

	$db->query($tsql);
	$district_list = $db->getRows();
	//海关分类
	$customs_cat = $pro->get_product_cat_customs();
	
	$tpl->assign("customs_cat",$customs_cat);
	
	$tpl->assign("firstvalue",$extfiled);
	$tpl->assign("district_list",$district_list);
	$tpl->assign("property_id",$property_id);
	$tpl->assign("abc",$abc);
	include_once("footer.php");
	$tpl->assign("config",$config);
	$tpl->assign("lang",$lang);
	$tpl->display("product_launch.htm");
}

?>